package ModelLayer;
import java.util.*;
import java.io.*;


/**
 * Write a description of class AddressBook here.
 * 
 * @author (Group1:Georgi,Roland and Mindaugas) 
 * @version (a version number or a date)
 */
public class AddressBook 
{
    private ArrayList<Person> persons;
    private static AddressBook addressBook ;
    
    
    private AddressBook()                          
    {
        persons = new ArrayList<Person>();
        
    }
    
    /**
     * Get the current address book
     * @return the AddressBook
     */
    public static AddressBook getAddressBook()
    {     
       if(addressBook==null){
          addressBook=new AddressBook();
        }
       return  addressBook;  
    }
    
    /**
     * Adds a person to the addressbook.
     */
    private void addPerson(Person person)            
    {
        persons.add(person);
    }
    
    /**
     * Checks if a person exists in the addressbook by searching for it usings its ID.
     */
    public boolean personExist(long id)
    {
        if(searchPerson(id)==null)
          return false;
          
        else
           return true;
    
    }
    
    /**
     * Searches for a person using he's ID.
     */
    public Person searchPerson(long id)
    {
       for(int i = 0; i<persons.size(); i++){
           
           if(persons.get(i).getID()==id){
               return persons.get(i);
            }
           
        }
        return null;
    }
    
    /**
     *Creates a new person with a new id. It also prevents creating two persons with the same ID
     */
    public void createPerson(long ID, String name, String address, String postalCode, String city, String phone)
    {
      if(searchPerson(ID) == null)
      {
      Person person = new Person(ID, name, address, postalCode, city, phone);
      addPerson(person);
      }
      
      else
        throw new RuntimeException();
    }
    
    /**
     * Searches through all the persons and removes the person with the specified ID.
     */
     public void deletePerson(long ID)
    {
        if(searchPerson(ID)!=null)
          persons.remove(searchPerson(ID));
          
        else
         throw new RuntimeException();
    }
    
    /**
     * Retruns all the Persons names.
     */
    public ArrayList<String> getPersonsNames()
    {
        ArrayList<String> names = new ArrayList<String>();
        
        for(int index=0; index<persons.size();index++)
           names.add(persons.get(index).getName());
           
        return names;
    }
    
    /**
     * Returns all the Persons Addresses.
     */
    public ArrayList<String> getPersonsAddress()
    {
        ArrayList<String> addresses = new ArrayList<String>();
        
        for(int index=0; index<persons.size(); index++)
           addresses.add(persons.get(index).getAddress());
           
        return addresses;
    }
    
    /**
     * Returns all the Persons Postal Codes.
     */
    public ArrayList<String> getPersonsPostalCodes()
    {
        ArrayList<String> postalCodes = new ArrayList<String>();
        
        for(int index=0; index<persons.size(); index++)
           postalCodes.add(persons.get(index).getPostalCode());
           
        return postalCodes;
    }
    
    /**
     * Returns all the Persons Cities.
     */
    public ArrayList<String> getPersonsCities()
    {
        ArrayList<String> cities = new ArrayList<String>();
        
        for(int index=0; index<persons.size(); index++)
          cities.add(persons.get(index).getAddress());
          
        return cities;
    }
    
    /**
     * Returns all the Persons Phones.
     */
    public ArrayList<String> getPersonsPhones()
    {
        ArrayList<String> phones = new ArrayList<String>();
        
        for(int index=0; index<persons.size(); index++)
            phones.add(persons.get(index).getAddress());
            
        return phones;
    }
    
    /**
     * Returns all the Persons IDs.
     */
     public ArrayList<Long> getPersonsIDs()
    {
        ArrayList<Long> ids = new ArrayList<Long>();
        
        for(int index=0; index<persons.size(); index++)
            ids.add(persons.get(index).getID());
            
        return ids;
    }
    
}
